package com.why.serviceedu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.why.serviceedu.bean.excel.subjectData;
import com.why.serviceedu.bean.oneSubject;
import com.why.serviceedu.bean.twoSubejct;
import com.why.serviceedu.listener.ExcelListener;
import com.why.serviceedu.mapper.EduSubjectMapper;
import com.why.serviceedu.service.EduSubjectService;
import com.why.serviceedu.bean.EduSubject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author why
 * @since 2021-01-31
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void savSubject(MultipartFile file,EduSubjectService service) {
        System.out.println(file.getOriginalFilename());
        try {

            EasyExcel.read(file.getInputStream(), subjectData.class,
                    new ExcelListener(service)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @Override
    public List<oneSubject> getOneAndTwoSubject() {
        QueryWrapper<EduSubject> wrapper=new QueryWrapper<>();
        wrapper.eq("parent_id",0);
        //使用源码中的baseMapper进行查询代码
        //查询一级分类
        List<EduSubject> eduSubjectsOne = baseMapper.selectList(wrapper);

        //查询二级分类
        QueryWrapper<EduSubject> wrapperTwo=new QueryWrapper<>();
        wrapper.ne("parent_id",0);
        //使用源码中的baseMapper进行查询代码
        List<EduSubject> eduSubjectsTwo = baseMapper.selectList(wrapperTwo);

        List<oneSubject> result=new ArrayList<>();
        for(EduSubject subjectOne:eduSubjectsOne){
            //一级分类对象
            oneSubject temp = new oneSubject();
            //一级分类的孩子，也就是二级分类
            List<twoSubejct> children=new ArrayList<>();
            //设置一级分类id和名字，可以使用beanUtils,前提是属性名字一样
            temp.setId(subjectOne.getId());
            temp.setTitle(subjectOne.getTitle());

           // BeanUtils.copyProperties(subjectOne,temp);
            //遍历二级分类，
            for(EduSubject subjectTwo:eduSubjectsTwo){
                //找自己的孩子
                if(subjectOne.getId().equals(subjectTwo.getParentId())){
                    twoSubejct twoSubejct = new twoSubejct();
                    twoSubejct.setId(subjectTwo.getId());
                    twoSubejct.setTitle(subjectTwo.getTitle());
                    children.add(twoSubejct);
                }
            }
            //设置孩子
            temp.setChildren(children);
            result.add(temp);
        }



        return result;
    }
}
